package api_base

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wrblog-api-go/app/common/token"
	"wrblog-api-go/app/model/model_base"
	"wrblog-api-go/app/service/service_base"
	"wrblog-api-go/pkg/request"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

// @Tags  Base - 文章库管理
// @Summary  获取库列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleLibrary query model_base.SelectBaseArticleLibrary true "baseArticleLibrary"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleLibrary/page [get]
func ApiArticleLibraryPageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleLibrary := raw.(model_base.SelectBaseArticleLibrary)
	result.PageRes(service_base.GetArticleLibraryPageList(baseArticleLibrary)).Json(c)
}

// @Tags  Base - 文章库管理
// @Summary  获取当前用户的库列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleLibrary query model_base.SelectBaseArticleLibrary true "baseArticleLibrary"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleLibrary/myPage [get]
func ApiMyArticleLibraryPageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleLibrary := raw.(model_base.SelectBaseArticleLibrary)
	baseArticleLibrary.UserId = token.GetUserId(c)
	result.PageRes(service_base.GetArticleLibraryPageList(baseArticleLibrary)).Json(c)
}

// @Tags  Base - 文章库管理
// @Summary  获取库详情
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param libraryId path int true "libraryId"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleLibrary/{libraryId} [get]
func ApiArticleLibraryInfo(c *gin.Context) {
	libraryId, err := strconv.Atoi(c.Param("libraryId"))
	if err != nil {
		result.Wrap(status.BadRequest, "参数读取失败").Json(c)
	}
	result.Res(service_base.GetArticleLibraryInfo(libraryId)).Json(c)
}

// @Tags  Base - 文章库管理
// @Summary  添加库
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleLibrary body model_base.BaseArticleLibrary true "baseArticleLibrary"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleLibrary/add [post]
func ApiAddArticleLibrary(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleLibrary := raw.(model_base.BaseArticleLibraryPo)
	loginUser := token.GetLoginUser(c)
	baseArticleLibrary.UserId = loginUser.UserId
	baseArticleLibrary.CreateBy = loginUser.Uk
	result.Res(service_base.AddArticleLibrary(baseArticleLibrary)).Json(c)
}

// @Tags  Base - 文章库管理
// @Summary  修改库
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleLibrary body model_base.BaseArticleLibrary true "baseArticleLibrary"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleLibrary/edit [put]
func ApiEditArticleLibrary(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleLibrary := raw.(model_base.BaseArticleLibraryPo)
	baseArticleLibrary.UpdateBy = token.GetUk(c)
	result.Res(service_base.EditArticleLibrary(baseArticleLibrary)).Json(c)
}

// @Tags  Base - 文章库管理
// @Summary  删除库（逻辑删除）
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param libraryIds query string true "libraryIds"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleLibrary/removes [delete]
func ApiRemoveArticleLibrary(c *gin.Context) {
	libraryIds := c.Query("libraryIds")
	result.Res(service_base.RemoveArticleLibrary(libraryIds, token.GetUk(c))).Json(c)
}
